package com.honestpeak.datafusion.service.impl;


import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.honestpeak.core.datasource.DataSourceContextHolder;
import com.honestpeak.core.datasource.DataSourceType;
import com.honestpeak.core.enums.MonthEnum;
import com.honestpeak.core.utils.StreamUtils;
import com.honestpeak.core.utils.WebUtils;
import com.honestpeak.core.utils.WeekUtils;
import com.honestpeak.datafusion.enums.YearTermEnum;
import com.honestpeak.datafusion.mapper.HpamsMapper;
import com.honestpeak.datafusion.model.*;
import com.honestpeak.datafusion.service.HpamsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Auther: apple
 * @Date: 2018/8/11 11:10
 * @Description:
 */
@Service
public class HpamsServiceImpl implements HpamsService {

    @Autowired
    private HpamsMapper hpamsMapper;

    @Override
    public Map<String, Object> getXyStuNumByDay(String day) {
        Map<String, Object> resultMap = Maps.newHashMap();
        List<HpamsDayData> resultList = Lists.newArrayList();
        List<Map<String, Object>> itemsList = Lists.newArrayList();
        if(StringUtils.isEmpty(day)){
            day = WebUtils.DateToString(new Date());
        }
        List<HpamsDayData> hpamsDayDataList = hpamsMapper.getXyStuNumByDay(day);
        resultList = hpamsDayDataList.stream().filter(x -> StringUtils.isNotEmpty(x.getDwmc())).collect(Collectors.toList());
        resultList.stream().forEach(
                x -> {
                    Map<String, Object> map = Maps.newHashMap();
                    map.put("name", x.getDwmc());
                    map.put("value", x.getStudentCount());
                    itemsList.add(map);
                }
        );
        resultMap.put("day", day);
        resultMap.put("items", itemsList);
        return resultMap;
    }

    /**
     * 根据学年查询莫院系下的所有晨跑学生数量
     * @param year
     * @param dwh
     * @return
     */
    @Override
    public List<Map<String, Object>> getYXStuNumByYear(String year, String dwh) {
        if(StringUtils.isEmpty(year)){
            year = WebUtils.getCourseYear(new Date());
        }
        Map<String, Object> cpResultMap = Maps.newHashMap();
        Map<String, Object> jbResultMap = Maps.newHashMap();
        List<Map<String, Object>> resultMapList = Lists.newArrayList();
        //查询晨跑数据
        List<DepartTermActivityCount> resultList = Lists.newArrayList();
        DataSourceContextHolder.setDbType(DataSourceType.HPAMS);
        List<DepartTermActivityCount> departTermActivityCounts = hpamsMapper.getYXStuNumByYear(year, dwh);
        resultList = departTermActivityCounts.stream().filter(x -> StringUtils.isNotEmpty(x.getYxmc())).collect(Collectors.toList());
        List<Map<String, Object>> cpList = Lists.newArrayList();
        resultList.stream().forEach(x -> {
            Map<String, Object> cpMap = Maps.newHashMap();
            if (x.getTerm() == YearTermEnum.YEAR_TERM_PREV.getCode()) {
                cpMap.put("term", YearTermEnum.YEAR_TERM_PREV.getValue());
                cpMap.put("value", x.getValue());

            } else {
                cpMap.put("term", YearTermEnum.YEAR_TERM_NEXT.getValue());
                cpMap.put("value", x.getValue());
            }
            cpMap.put("yxmc", x.getYxmc());
            cpMap.put("year", x.getYear());
            cpList.add(cpMap);

        });
        cpResultMap.put("name", "晨跑");
        cpResultMap.put("items", cpList);
        resultMapList.add(cpResultMap);
        //TODO 健步暂时没有
        jbResultMap.put("name", "健步");
        jbResultMap.put("items", "");
        resultMapList.add(jbResultMap);
        return resultMapList;
    }

    /**
     * 获取院系信息
     * @return
     */
    public List<Dwh> getDwInfo(){
        List<Dwh> dwhList = hpamsMapper.getDwInfo();
        dwhList.stream().filter(x -> x.getDwh() != null).collect(Collectors.toList());
        return dwhList;
    }

    /**
     * 获取学年学期信息
     * @return
     */
    @Override
    public List<String> getYearTermInfo() {
        List<YearTerm> yearTermList = hpamsMapper.getYearTermInfo();
        return yearTermList.stream().map(YearTerm::getYear).distinct().collect(Collectors.toList());
    }

    /**
     * 晨跑月活动人数
     * @param year
     * @param dwh
     * @return
     */
    @Override
    public List<Map<String, Object>> getMonthRunActiveCountByYearDwh(String year, Integer dwh) {
        List<Integer> monthList = WebUtils.AssemblyMonth();
        List<Map<String, Object>> cpItemsList = Lists.newArrayList();
        monthList.stream().forEach(x -> {
            HpamsRunMonthActive hpamsRunMonthActive = hpamsMapper.getMonthRunActiveCountByYearDwh(year, dwh, x);
            Map<String, Object> cpItemsMap = Maps.newHashMap();
            cpItemsMap.put("month", getMonthStr(x));
            cpItemsMap.put("value", hpamsRunMonthActive.getValue());
            cpItemsList.add(cpItemsMap);
        });
        List<Map<String, Object>> resultMapList = Lists.newArrayList();
        Map<String, Object> cpResultMap = Maps.newHashMap();
        cpResultMap.put("cp", "晨跑");
        cpResultMap.put("items", cpItemsList);
        Map<String, Object> jbResultMap = Maps.newHashMap();
        //TODO 健步暂时没有
        jbResultMap.put("jb", "健步");
        jbResultMap.put("items","");
        resultMapList.add(cpResultMap);
        return resultMapList;
    }

    @Override
    public List<Map<String, Object>> getHpamsActiveCountByYearWeekDwh(String year, String dwh) {
        List<Map<String, Object>> result = Lists.newArrayList();
        if(StringUtils.isEmpty(year)){
            year = WebUtils.DateToYearStr(new Date());
        }

        DataSourceContextHolder.setDbType(DataSourceType.HPAMS);
        Map<String, Object> cpResultMap =  Maps.newHashMap();
        List<Map<String, Object>> cpMapList = Lists.newArrayList();
        //获取一年周数
        int weekNum = WebUtils.getWeekOfYear(Integer.valueOf(year));
        for(int i = 1; i<= weekNum; i++){
            Map<String , Object> weekMap = Maps.newHashMap();
            //开始日期，结束日期
            String weekStart = WeekUtils.getStartDayOfWeekNo(Integer.valueOf(year), i);
            String weekEnd = WeekUtils.getEndDayOfWeekNo(Integer.valueOf(year), i);
            //
            BaseModel baseModel = hpamsMapper.getHpamsActiveCountByYearWeekDwh(dwh, weekStart, weekEnd);
            weekMap.put("name", WeekUtils.getWeeks(i));
            weekMap.put("value", baseModel.getValue());
            cpMapList.add(weekMap);

            //TODO 健步
        }
        cpResultMap.put("name", "晨跑");
        cpResultMap.put("items", cpMapList);
        result.add(cpResultMap);
        DataSourceContextHolder.clearDbType();
        return result;
    }

    /**
     * @param month
     * @return
     */
    private String getMonthStr(Integer month){
        String monthStr = "";
        if(month == MonthEnum.MONTH_ENUM_JANUARY.getCode()){
            monthStr = MonthEnum.MONTH_ENUM_JANUARY.getValue();
        } else if(month == MonthEnum.MONTH_ENUM_FEBRUARY.getCode()){
            monthStr = MonthEnum.MONTH_ENUM_FEBRUARY.getValue();
        }else if(month == MonthEnum.MONTH_ENUM_MARCH.getCode()){
            monthStr =MonthEnum.MONTH_ENUM_MARCH.getValue();
        }else if(month == MonthEnum.MONTH_ENUM_APRIL.getCode()){
            monthStr = MonthEnum.MONTH_ENUM_APRIL.getValue();
        }else if(month == MonthEnum.MONTH_ENUM_MAY.getCode()){
            monthStr = MonthEnum.MONTH_ENUM_MAY.getValue();
        }else if(month == MonthEnum.MONTH_ENUM_JUNE.getCode()){
            monthStr = MonthEnum.MONTH_ENUM_JUNE.getValue();
        }else if(month == MonthEnum.MONTH_ENUM_JULY.getCode()){
            monthStr = MonthEnum.MONTH_ENUM_JULY.getValue();
        }else if(month == MonthEnum.MONTH_ENUM_AUGUST.getCode()){
            monthStr = MonthEnum.MONTH_ENUM_AUGUST.getValue();
        }else if(month == MonthEnum.MONTH_ENUM_SEPTEMBER.getCode()){
            monthStr = MonthEnum.MONTH_ENUM_SEPTEMBER.getValue();
        }else if(month == MonthEnum.MONTH_ENUM_OCTOBER.getCode()){
            monthStr = MonthEnum.MONTH_ENUM_OCTOBER.getValue();
        } else if(month == MonthEnum.MONTH_ENUM_NOVEMBER.getCode()){
            monthStr = MonthEnum.MONTH_ENUM_NOVEMBER.getValue();
        } else if(month == MonthEnum.MONTH_ENUM_DECEMBER.getCode()){
            monthStr = MonthEnum.MONTH_ENUM_DECEMBER.getValue();
        }
        return monthStr;
    }
}
